package com.resou.reader.bookstore.category;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.resou.reader.R;
import com.resou.reader.api.entry.SortSubBean;
import com.resou.reader.base.v.BaseFragment;
import com.resou.reader.bookshelf.listener.OnItemClickListener;
import com.resou.reader.bookstore.category.adapter.CategoryAdapter;
import com.resou.reader.view.ItemDecoration;

import java.util.List;

import butterknife.BindView;

/**
 * Created by ${Garrett} on 2018/9/29.
 * Contact me krouky@outlook.com
 */
public class AllCategoryFragment extends BaseFragment {

    private AllCategoryActivity.OnClickedItem onItemClickListener;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private int index;
    private List<SortSubBean> data;

    private CategoryAdapter mAdapter = new CategoryAdapter(new OnItemClickListener() {
        @Override
        public void onItemClicked(RecyclerView.Adapter adapter, View itemView, int position) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(index, position);
            }
        }
    });

    public void setOnItemClickListener(AllCategoryActivity.OnClickedItem onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_all_catelgory;
    }

    @Override
    protected void initViews() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getViewContext(), 2));
        mRecyclerView.addItemDecoration(new ItemDecoration(20, 40));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public Context getViewContext() {
        return getContext();
    }

    public void setData(List<SortSubBean> data) {
        this.data = data;
        mAdapter.setData(data);
    }
}
